//商家店铺列表
<template>
   <div class="search">
    <Row>
      <Col>
        <Card>
          <Row class="operation">
             <Form inline :label-width="80" ref="searchForm" :model="searchForm" label-position="right">
              <Button @click="getDataList" icon="md-refresh">刷新</Button>
               <FormItem label="店铺名称" prop="username">
                 <Input v-model="searchForm.username" placeholder="请输入账号名称" />
               </FormItem>
               <FormItem label="联系方式" prop="mobile">
                 <Input v-model="searchForm.mobile" placeholder="请输入商家联系方式" />
               </FormItem>
               <Form-item label="是否严选" prop="isBest">
                <Select v-model="searchForm.isBest" style="width:200px" clearable>
                  <Option value="0">不严选</Option>
                  <Option value="1">严选</Option>
                </Select>
              </Form-item>
               <FormItem label="店铺地址" prop="address" style="padding-left:50px">
                 <Input v-model="searchForm.address" placeholder="请输入商家地址" />
               </FormItem>
               <Button @click="handleSearch" type="primary" icon="ios-search" style="margin-left:50px;">搜索</Button>
              </Form>
          </Row>
          <!-- <Row>
            <Alert show-icon>
              <a class="select-clear" @click="clearSelectAll">清空</a>
            </Alert> 
          </Row> -->
          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>

        <!-- 商家详情 -->
        <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="700">
          <div style="font-size: 14px;padding: 5px 0;"><label>店铺名称：</label>{{detailData.shopName}}</div>
          <div style="font-size: 14px;padding: 5px 0;"><label>联系方式：</label>{{detailData.mobile}}</div>
          <div style="font-size: 14px;padding: 5px 0;"><label>支付宝账号：</label>{{detailData.alipayAccount}}</div>
          <div style="font-size: 14px;padding: 5px 0;"><label>微信账号：</label>{{detailData.wechatAccount}}</div>
          <div style="font-size: 14px;padding: 5px 0;"><label>服务类型：</label>{{detailData.className}}</div>
          <div style="font-size: 14px;padding: 5px 0;"><label>所在城市：</label>{{detailData.areaName}}</div>
          <div style="font-size: 14px;padding: 5px 0;"><label>店铺地址：</label>{{detailData.address}}</div>
          <div style="font-size: 14px;padding: 5px 0;height: 200px;">
            <label>身份证正面：</label>
            <img :src="detailData.idCardFront" alt="" style="max-height: 100%;max-width: 100%;">
          </div>
          <div style="font-size: 14px;padding: 5px 0;height: 200px;">
            <label>身份证反面：</label>
            <img :src="detailData.idCardBack" alt="" style="max-height: 100%;max-width: 100%;">
          </div>
          <div style="font-size: 14px;padding: 5px 0;height: 200px;">
            <label>营业执照：</label>
            <img :src="detailData.businessCard" alt="" style="max-height: 100%;max-width: 100%;">
          </div>
        </Modal>

        <!-- 商家账户余额 -->
        <Modal :title="modalTitle" v-model="balanceVisible" :mask-closable='false' :width="700">
          <div style="font-size: 14px;padding: 5px 0;"><label>账户余额：</label>{{balanceData}}</div>
        </Modal>

        <!-- 商家商品信息 -->
        <Modal title="商品列表" v-model="goodsVisible" :mask-closable='false' :width="1100">
          <Row>
            <Table :loading="loading" border :columns="goodsColumns" :data="goodsData" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="goodsForm.pageNumber" :total="goodsTotal" :page-size="goodsForm.pageSize" @on-change="changeGoodsPage" @on-page-size-change="changeGoodsPageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Modal>

        <Modal title="编辑商品信息" v-model="editVisible" :mask-closable='false' :width="700">
          <Form ref="addForm" :model="addForm" :label-width="100" :rules="formRules">
            <FormItem label="商品名称" prop="goodName">
              <Input v-model="addForm.goodName" placeholder="请输入商品名称" />
            </FormItem>
            <FormItem label="商品分类" prop="goodCategoryId">
              <Poptip trigger="click" placement="right-start" title="分类" width="100%">
                <Input v-model="addForm.goodCategoryName" style="width:100%" readonly placeholder="请选择商品分类" />
                <div slot="content" style="position:relative;min-height:5vh">
                  <Tree :data="classifyData" :load-data="loadData" @on-select-change="selectTwoTreeEdit" ref="treeTwo"></Tree>
                  <Spin size="large" fix v-if="loadingEdit"></Spin>
                </div>
              </Poptip> 
            </FormItem>
            <FormItem label="商品原价" prop="goodOldPrice">
              <Input v-model="addForm.goodOldPrice" placeholder="请输入商品名称" />
            </FormItem>
            <FormItem label="商品现价" prop="goodNewPrice">
              <Input v-model="addForm.goodNewPrice" placeholder="请输入商品名称" />
            </FormItem>
            <FormItem label="积分" prop="integral">
              <Input v-model="addForm.integral" placeholder="请输入积分" />
            </FormItem>
            <FormItem label="商品图片" prop="goodPics">
              <upload-pic-input v-model="addForm.goodPics"></upload-pic-input>
            </FormItem>
            <FormItem label="商品详情">
              <editor id="editor" v-model="addForm.goodDetails" ref="editor"></editor>
            </FormItem>
          </Form>
          <div slot="footer">
            <Button type="text" @click="editVisible = false">取消</Button>
            <Button type="primary" @click="handleSubmit">提交</Button>
          </div>
        </Modal>

        <Modal title="查看大图" v-model="visible" :width="600">
          <img :src="imgSrc" style="width: 100%">
        </Modal>

        <Modal
          v-model="videoVisible"
          title="视频播放"
          :width="600"
          @on-cancel="closeVideo"
          draggable
        >
          <div id="dplayer" style="height: 500px"></div>
        </Modal>
      </Col>
    </Row>
    </div> 
</template>

<script>
import uploadPicThumb from "@/views/my-components/xboot/upload-pic-thumb";
import uploadPicInput from "@/views/my-components/xboot/upload-pic-input";
import editor from "@/views/my-components/xboot/editor";
import {userInfo} from "@/api/index.js"
import {getCategoryList} from "@/api/commodity.js"
import "dplayer/dist/DPlayer.min.css";
import DPlayer from "dplayer";
var dp
export default {
  components: {
    uploadPicInput,
    uploadPicThumb,
    editor
  },
    data(){
      return{
        videoVisible: false,
        imgSrc: "",
        visible: false,
        submitLoading: false, // 添加或编辑提交状态
        modalVisible:false,
        balanceVisible: false, // 账户余额弹窗
        goodsVisible: false, // 商品列表弹窗
        loading:true,
        editVisible: false,
        total:0,
        modalTitle: "", //
        data:[],//表单数据
        searchForm:{
          pageNumber:1,
          pageSize:10,
          username:'',
          mobile:'',
          isBest:'',
          address:'',
        },
        goodsForm: {
          pageNumber: 1,
          pageSize: 10,
          storeId: ""
        },
        addForm: {
          id: "",
          goodCategoryId: "",
          goodName: "",
          goodPics: "",
          goodDetails: "",
          goodOldPrice: "",
          goodNewPrice: "",
          integral: "",
          typeEnum: "GOOD"
        },
        formRules: {
          goodName: [{ required: true, message: "请输入商品名称", trigger: "blur" },
            { type: 'string', max: 30, message: '商品名称不能多于30字', trigger: 'blur' }],
          goodCategoryId: [{ required: true, message: "请选择商品分类", trigger: "change" }],
          goodPics: [{ required: true, message: "请选择商品轮播", trigger: "blur", type: "array" }],
          goodNewPrice: [{ required: true, message: "请输入商品原价", trigger: "blur",pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/ },
                    { required: true, message: "商品原价格式不正确",pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/}],
          goodOldPrice: [{ required: true, message: "请输入商品现价", trigger: "blur",pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/ },
                    { required: true, message: "商品现价格式不正确",pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/}],
          integral: [{ required: true, message: "请输入积分", trigger: "blur", type: "number" },
                    { required: true, message: "商品积分只能为正整数", trigger: "blur", type: "number" }],
        },
        goodsData: [],
        goodsTotal: 0,
        columns: [
          // 表头
          {
            type: "index",
            width: 60,
            align: "center"
          },
          {
            title: "店铺名称",
            key: "shopName",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "联系方式",
            key: "mobile",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "支付宝账号",
            key: "alipayAccount",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "微信账号",
            key: "wechatAccount",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "服务类型",
            key: "className",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "店铺地址",
            key: "address",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "所属城市",
            key: "areaName",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "创建时间",
            key: "createTime",
            minWidth: 120,
            sortable: false,
          },
          {
            title: "操作",
            key: "action",
            width: 320,
            align: "center",
            fixed: "right",
            render: (h, params) => {
              return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small"
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.detail(params.row);
                      }
                    }
                  },
                  "详情"
                ),     
                h(
                  "Button",
                  {
                    props: {
                      type: "default",
                      size: "small"
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.accountBalance(params.row);
                      }
                    }
                  },
                  "账户余额"
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "info",
                      size: "small"
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.goodsInfo(params.row);
                      }
                    }
                  },
                  "商品信息"
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "info",
                      size: "small"
                    },
                    style: {
                      marginRight: "5px",
                      display:(params.row.isBest==1)?"none":"inline-block"
                    },
                    on: {
                      click: () => {
                        this.disable(params.row);
                      }
                    }
                  },
                  "设为严选"
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "default",
                      size: "small"
                    },
                    style: {
                      marginRight: "5px",
                      display:(params.row.isBest==0)?"none":"inline-block"
                    },
                    on: {
                      click: () => {
                        this.enable(params.row);
                      }
                    }
                  },
                  "取消严选"
                ),
              ]);
            }
          }
        ],
        // commodityData: [],
        goodsColumns: [
        {
          type: "index",
          width: 60,
          align: "center",
          fixed: "left"
        },
        {
          title: "商品名称",
          minWidth: 120,
          sortable: false,
          key: "goodName"
        },
        {
          title: "商家名称",
          minWidth: 120,
          sortable: false,
          key: "shopName"
        },  
        {
          title: "商品图片",
          minWidth: 120,
          sortable: false,
          key: "goodPics",
          render: (h, params) => {
            var imgs = params.row.goodPics.split(",")
            return h("img", {
              attrs: {
                src: imgs[0]
              },
              style: {
                width: "40px",
                height: "30px"
              },
              on: {
                click: ()=> {
                  this.handleView(imgs[0])
                }
              }
            });
          }
        },
        {
          title: "视频展示图",
          minWidth: 120,
          sortable: false,
          key: "goodVideoPic",
          render: (h, params) => {
            return h("img", {
              attrs: {
                src: params.row.goodVideoPic
              },
              style: {
                width: "40px",
                height: "30px"
              },
              on: {
                click: ()=> {
                  this.handleView(params.row.goodVideoPic)
                }
              }
            });
          }
        }, 
        {
          title: "商品视频",
          minWidth: 120,
          sortable: false,
          key: "goodVideo",
          render: (h, params) => {
            return h("video",
              {
                style: {
                  cursor: "pointer",
                  width: "80px",
                  height: "60px",
                  margin: "10px 0",
                  "object-fit": "contain"
                },
                on: {
                  click: () => {
                    this.showVideo(params.row);
                  }
                }
              },
              [
                h("source", {
                  attrs: {
                    src: params.row.goodVideo
                  }
                })
              ]
            );
          }
        },      
        {
          title: "商品原价",
          minWidth: 120,
          sortable: false,
          key: "goodOldPrice"
        },
        {
          title: "商品现价",
          minWidth: 120,
          sortable: false,
          key: "goodNewPrice"
        },
        {
          title: "所属分类",
          minWidth: 120,
          sortable: false,
          key: "goodCategoryName"
        },
        {
          title: "购买状态",
          minWidth: 120,
          sortable: false,
          key: "buyState",
          render: (h,params) => {
            if (params.row.buyState === 1) {
              return h("span", "可购买")
            } else if (params.row.buyState === 2) {
              return h ("span", "可租赁")
            } else if (params.row.buyState === 3) {
              return h ("span", "可购买和租赁")
            }
          }
        },
        {
          title: "租赁状态",
          minWidth: 120,
          sortable: false,
          key: "leaseState",
          render: (h,params) => {
            if (params.row.leaseState === 1) {
              return h ("span", "线上支付尾款")
            } else if (params.row.leaseState === 2) {
              return h ("span", "线下支付尾款")
            }
          }
        },
        {
          title: "创建时间",
          minWidth: 120,
          sortable: false,
          key: "createTime"
        },        
        {
          title: "操作",
          width: 100,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.delFlag==1)?"none":"inline-block"
                  },
                  on: {
                    click: () => {
                      this.frozen(params.row,1);
                    }
                  }
                },
                "禁用"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.delFlag==0)?"none":"inline-block"
                  },
                  on: {
                    click: () => {
                      this.frozen(params.row,2);
                    }
                  }
                },
                "启用"
              ),
            ]);
          }
        }
      ],
        detailData: {},
        balanceData: {}, // 商家账户余额
        storeId: "",
        classifyData: [],
        loadingEdit: false,
        userRole: ""
      }
    },
    methods:{
      init(){
        this.getDataList();
        this.getUserRole();
        this.getCategory();
      },

      getUserRole () {
        userInfo().then(res=> {
          if (res.success) {
            this.userRole = res.result.type;
          }
        })
      },

      showVideo(v) {
        dp = new DPlayer({
          container: document.getElementById("dplayer"),
          screenshot: true,
          video: {
            url: v.goodVideo
          },
        });
        this.videoVisible = true;
      },
      closeVideo() {
        dp.destroy();
      },

      handleView(src) {
          console.log(src)
        this.visible = true;
        this.imgSrc = src;
      },

      // 获取分类列表
    getCategory() {
      getCategoryList().then(res => {
        if (res.success) {
          res.result.forEach(function(e) {
            e.title=e.title;
              if (e.isParent) {
                e.loading = false;
                e.children = [];
              }
            });
          this.classifyData = res.result;
        }
      });
    },

    loadData(item, callback) { 
        // 异步加载树子节点数据
      var param ={
        pid:item.id
      }
      this.postRequest("/goodCategory/getGoodCategoryList",param).then(res => {
        if (res.success) {
          res.result.forEach(function(e) {
            e.title=e.title
            if (e.isParent) {
              e.loading = false;
              e.children = [];
            }
          });
          callback(res.result);
        }
      });
    },

    selectTwoTreeEdit(v){
      console.log(v);
      if (v[0].parentId === "0") {
        this.$Message.warning("添加或修改商品只能选择子节点")
      } else {
        var data = {
          pid:v[0].id
        }
        this.addForm.goodCategoryId = v[0].id;
        this.addForm.goodCategoryName = v[0].title;
        this.$forceUpdate();
      }
    },

      // 商家详情
      detail (v) {
        this.modalTitle = "商家详情";
        var data = {
          userId: v.id,
          userTypeEnum: "STORE"
        }
        this.postRequest("/user/getBackUserParticulars", data).then(res=> {
          if (res.success) {
            this.detailData = res.result;
          } else {
            this.detailData = {}
          }
        })
        this.modalVisible = true;
      },

      // 商家账户余额
      accountBalance (v) {
        this.modalTitle = "账户余额";
        var data = {
          userId: v.id
        }
        this.postRequest("/appUser/getAccountBalance", data).then(res=> {
          if (res.success) {
            this.balanceData = res.result;
          } else {
            this.balanceData = {}
          }
        })
        this.balanceVisible = true;
      },

      // 商品信息
      goodsInfo (v) {
        this.goodsForm.storeId = v.id;
        this.loading = true;
        this.postRequest("/good/PCgoodsList", this.goodsForm).then(res=> {
          this.loading = false;
          if (res.success) {
            this.goodsData = res.result.data;
            this.goodsTotal = res.result.size
          } else {
            this.goodsData = [];
            this.goodsTotal = 0;
          }
        })
        this.goodsVisible = true;
      },

      //取消严选
      enable(v){
        this.$Modal.confirm({
          title: "提示",
          // 记得确认修改此处
          content: "您确认要取消严选?",
          loading: true,
          onOk: () => {
            let param={
              ids:v.id
            }
            this.postRequest("/selectStore/removeSelectStore",param).then(res => {
              this.$Modal.remove();
              if (res.success) {
                this.$Message.success("取消成功");
                this.getDataList();
              }
            });
          }
        });
      },

      edit (v) {
        this.form.id = v.id;
        this.addForm.goodName = v.goodName;
        this.addForm.goodCategoryId = v.goodCategoryId;
        this.addForm.goodCategoryName = v.goodCategoryName;
        this.addForm.goodPics = v.goodPics;
        this.addForm.goodNewPrice = v.goodNewPrice;
        this.addForm.goodOldPrice = v.goodOldPrice;
        this.$refs.editor.setData(v.goodDetails);
        this.addForm.integral = v.integral;
        this.editVisible = true;
      },

      handleSubmit () {
        this.$refs.addForm.validate(valid => {
          if (valid) {
            this.postRequest("/good/insertOrderUpdateGood",this.addForm).then(res=> {
              if (res.success) {
                this.$Message.success("操作成功");
                var data = {
                  storeId: this.storeId,
                  pageNumber: 1,
                  pageSize: 10
                }
                this.editVisible = false;
                this.loading = true;
                this.postRequest("/good/PCgoodsList", data).then(res=> {
                  this.loading = false;
                  if (res.success) {
                    this.goodsData = res.result.data;
                    this.goodsTotal = res.result.size
                  } else {
                    this.goodsData = [];
                    this.goodsTotal = 0;
                  }
                })
              }
            })
          }
        })
      },

      //清空
      clearSelectAll(){
        this.$refs.searchForm.resetFields()
      },

      //搜索
      handleSearch(){
        if (this.searchForm.isBest == undefined) {
          this.searchForm.isBest = ""
        }
        this.searchForm.pageNumber = 1;
        this.searchForm.pageSize = 10;
        this.getDataList()
      },

      //获取商家列表
      getDataList(){
        this.loading = true
        this.postRequest("/user/getBackUserStoreList",this.searchForm).then(res =>{
          this.loading = false
          if(res.success){
            var arr = res.result.data;
            for(let val of arr){
              for(let i in val){
                (val[i] === null) ? (val[i] = "-") : ""
              }
            }
            this.data = res.result.data;
            this.total = res.result.size;
          } else {
            this.data = [];
            this.total = 0;
          }
        })
      },

      // 设为严选
      disable(v){
        this.$Modal.confirm({
          title: "确认设为严选",
          // 记得确认修改此处
          content: "您确认要设为严选?",
          loading: true,
          onOk: () => {
            let param={
              userId:v.id
            }
            console.log(param)
              this.postRequest("/selectStore/insertOrderUpdateSelectStore",param).then(res => {
              this.$Modal.remove();
              console.log(res)
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
              } else {
                this.$Message.success(res.message);
              }
            });
          }
        });
      },

      frozen(v, k) {
        console.log(v)
        let params ={
          id:v.id
        }
        this.postRequest("/good/removeGood", params).then(res=>{
          console.log(res)
          if(res.success){
            if (k===1) {
              this.$Message.success("冻结成功");
            } else {
              this.$Message.success("解冻成功");
            }
            this.loading = true;
            this.postRequest("/good/PCgoodsList", this.goodsForm).then(res=> {
              this.loading = false;
              if (res.success) {
                this.goodsData = res.result.data;
                this.goodsTotal = res.result.size
              } else {
                this.goodsData = [];
                this.goodsTotal = 0;
              }
            })
          } else {
            this.$Message.error(res.message)
          }
        })
      },

      changeSort(e) {
        this.searchForm.sort = e.key;
        this.searchForm.order = e.order;
        if (e.order === "normal") {
          this.searchForm.order = "";
        }
        this.getDataList();
      },
      changePage(v) {
        this.searchForm.pageNumber = v;
        this.getDataList();
      },
      changePageSize(v) {
        this.searchForm.pageSize = v;
        this.getDataList();
      },
      changeGoodsPage (v) {
        this.goodsForm.pageNumber = v;
        this.loading = true;
        this.postRequest("/good/PCgoodsList", this.goodsForm).then(res=> {
          this.loading = false;
          if (res.success) {
            this.goodsData = res.result.data;
            this.goodsTotal = res.result.size
          } else {
            this.goodsData = [];
            this.goodsTotal = 0;
          }
        })
      },
      changeGoodsPageSize (v) {
        this.goodsForm.pageSize = v;
        this.loading = true;
        this.postRequest("/good/PCgoodsList", this.goodsForm).then(res=> {
          this.loading = false;
          if (res.success) {
            this.goodsData = res.result.data;
            this.goodsTotal = res.result.size
          } else {
            this.goodsData = [];
            this.goodsTotal = 0;
          }
        })
      }
    },
    mounted(){
      this.init()
    }
}
</script>

<style lang="less" scoped>
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>